<template>
  <div class="table-wrap">
    <div class="search_header">
      <div class="search_header_left">
        <!-- v-if="isJcdw || isAdmin" -->
        <el-button
          @click="handleAddInspect"
          v-has="'reportManagement:add'"
          ><div style="display: inline-block">
            <Icon
              name="jiance"
              color="#FFFFFF"
              font-size="14"
            />
          </div>
          上传检测报告</el-button
        >
      </div>
      <div class="search_header_right">
        <el-select
          placeholder="状态"
          v-model="parameter.status"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.dictValue"
            :label="item.dictText"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        <el-input
          placeholder="请输入项目名称"
          clearable
          v-model="parameter.projectName"
          @input="search"
        >
          <span
            slot="suffix"
            style="display: inline-block"
            v-show="!parameter.projectName"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
      </div>
    </div>
    <el-table
      :data="reportList"
      :header-cell-style="{
        background: '#F2F6FB ',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        label="序号"
        show-overflow-tooltip
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="项目编号"
        prop="projectNo"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="项目名称"
        prop="projectName"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="标段名称"
        prop="sectionName"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="施工单位"
        prop="workUnit"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="检测单位"
        prop="deptName"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="状态"
        prop="statusDictText"
        show-overflow-tooltip
      />
      <el-table-column
        label="提交时间"
        prop="createTime"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="update(scope.row)"
            style="color: #00baff; font-weight: 400"
            v-if="
              showEdit(scope.row) &&
              $filterGlobalPermission(
                'reportManagement:edit'
              )
            "
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="change(scope.row)"
            style="color: #00baff; font-weight: 400"
            v-if="
              showChange(scope.row) &&
              $filterGlobalPermission(
                'reportManagement:change'
              )
            "
            >整改</el-button
          >
          <el-button
            type="text"
            @click="check(scope.row)"
            style="color: #00baff; font-weight: 400"
            v-if="
              showCheck(scope.row) &&
              $filterGlobalPermission(
                'reportManagement:check'
              )
            "
            >审核</el-button
          >
          <el-button
            type="text"
            @click="handleBack(scope.row)"
            style="color: #ff5b5b; font-weight: 400"
            v-if="
              showBack(scope.row) &&
              $filterGlobalPermission(
                'reportManagement:back'
              )
            "
            >退回</el-button
          >
          <el-button
            type="text"
            @click="view(scope.row)"
            style="color: #00baff; font-weight: 400"
            >查看
          </el-button>
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-if="
              showDetete(scope.row) &&
              $filterGlobalPermission(
                'reportManagement:delete'
              )
            "
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 报告选择弹窗 -->
    <inspectChoose ref="inspectChooseRef" />
    <!-- 审核弹窗 -->
    <checkDialog ref="checkRef" @success="getPageList" />
  </div>
</template>

<script>
import {
  isAdmin,
  isEnterprise,
  isJldw,
  isSgdw,
  isJcdw
} from '@/utils/permission'
import Icon from '@/components/icon'
import {
  queryProjectList,
  deleteProjectInfo
} from '@/api/reportManagement'
import inspectChoose from './components/inspectChoose.vue'
import { getDictOptions } from '@/utils/dict'
import checkDialog from './components/checkDialog.vue'

export default {
  name: 'index',
  components: {
    Icon,
    inspectChoose,
    checkDialog
  },
  data() {
    return {
      reportList: [],
      total: 0,
      parameter: {
        pageSize: 10,
        pageIndex: 1,
        status: '',
        projectName: ''
      },

      statusOptions: []
    }
  },
  watch: {},
  computed: {
    isAdmin() {
      return isAdmin()
    },
    isEnterprise() {
      return isEnterprise()
    },
    isJcdw() {
      return isJcdw()
    }
  },
  mounted() {
    this.initOptions()
    this.getPageList()
  },
  methods: {
    /**
     * 编辑按钮显示：
     * 检测单位 && 待提交状态
     * 管理员 && (待提交状态  || 审核通过状态)
     */
    showEdit(row) {
      if (isJcdw() && row.status === '0') {
        return true
      } else if (
        isAdmin() &&
        (row.status === '0' || row.status === '1')
      ) {
        return true
      } else {
        return false
      }
    },
    /**
     * 整改按钮显示：
     * 实施单位 && (待整改状态 || 退回修改状态）
     */
    showChange(row) {
      if (row.status === '2' || row.status === '-1') {
        return true
      } else {
        return false
      }
      // if (
      //   isSgdw() &&
      //   (row.status === '2' || row.status === '-1')
      // ) {
      //   return true
      // } else {
      //   return false
      // }
    },
    /**
     * 审核按钮显示：
     * 监理单位 && 待初审状态
     * 建设单位 && 待审核状态
     */
    showCheck(row) {
      if (isJldw() && row.status === '3') {
        return true
      } else if (isEnterprise() && row.status === '4') {
        return true
      } else {
        return false
      }
    },
    /**
     * 退回按钮显示：
     * 管理员 && 非待提交状态
     */
    showBack(row) {
      if (row.status !== '0') {
        return true
      } else {
        return false
      }
      // if (isAdmin() && row.status !== '0') {
      //   return true
      // } else {
      //   return false
      // }
    },
    /**
     * 删除按钮显示：
     * 管理员
     * 检测单位 && 待提交状态
     */
    showDetete(row) {
      if (isAdmin()) {
        return true
      } else if (row.status === '0') {
        return true
      } else {
        return false
      }
      // if (isAdmin()) {
      //   return true
      // } else if (isJcdw() && row.status === '0') {
      //   return true
      // } else {
      //   return false
      // }
    },
    async initOptions() {
      const data = await getDictOptions('zhjs_aq_status')
      this.statusOptions = data
    },
    update(e) {
      this.$router.push({
        path: '/wisdomConstruction/reportManagement/form',
        query: {
          projectId: e.projectId,
          pkid: e.pkid,
          type: 'edit',
          title: '编辑'
        }
      })
    },
    change(e) {
      this.$router.push({
        path: '/wisdomConstruction/reportManagement/form',
        query: {
          projectId: e.projectId,
          pkid: e.pkid,
          type: 'change',
          title: '整改'
        }
      })
    },
    view(e) {
      this.$router.push({
        path: '/wisdomConstruction/reportManagement/form',
        query: {
          projectId: e.projectId,
          pkid: e.pkid,
          type: 'detail',
          title: '详情'
        }
      })
    },
    check(e) {
      this.$router.push({
        path: '/wisdomConstruction/reportManagement/form',
        query: {
          projectId: e.projectId,
          pkid: e.pkid,
          type: 'check',
          title: '审核'
        }
      })
    },
    handleBack(e) {
      this.$refs.checkRef.show(e.pkid, e.status, true)
    },
    getPageList() {
      queryProjectList(this.parameter).then((res) => {
        if (res.success) {
          this.reportList = res.data.list
          this.total = res.data.count
        } else {
          this.$message.error(res.message)
        }
      })
    },
    search() {
      this.parameter.pageIndex = 1
      this.total = ''
      this.getPageList()
    },
    pageChange(page) {
      this.parameter.pageIndex = page
      this.getPageList()
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },
    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该项目的检测报告，是否继续？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteProjectInfo(row.pkid).then((res) => {
            if (res.success) {
              this.$message.success(res.data)
              if (
                this.total % 10 === 1 &&
                this.parameter.pageIndex ===
                  parseInt(this.total / 10) + 1 &&
                this.parameter.pageIndex !== 1
              ) {
                this.parameter.pageIndex -= 1
              }
              this.getPageList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },
    // 打开检查选择弹窗
    handleAddInspect() {
      this.$refs.inspectChooseRef.show()
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
